package org.jeecg.modules.dock.handler.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.job.service.SyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    sap的通用处理
 * 创建人员:    zhaojun
 * 创建时间:    2023-12-22
 */
@Service
@Slf4j
public class SapCommonService {

    @Autowired
    private IBasicMaterialService basicMaterialService;
    @Autowired
    private SyncService syncService;
    @Autowired
    private IWmsInReceiptService wmsInReceiptService;

    /**
     * 检验sap物料是否wms存在，不存在则同步物料
     * @param materialCodes
     */
    public void verifyExistenceMaterialCode(List<String> materialCodes){
        materialCodes = materialCodes.stream().distinct().collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(materialCodes)){
            long count = basicMaterialService.count(new LambdaQueryWrapper<BasicMaterial>().in(BasicMaterial::getCode, materialCodes));
            if(materialCodes.size() != count){
                syncService.syncMaterial(1);
            }
        }
    }

}
